//
//  TunerDetailViewController.m
//  TheRoadAD
//
//  Created by 宁宁 on 16/8/11.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "TunerDetailViewController.h"

#import "TunerDetailHeaderView.h"//tableView页眉View
#import "TunerDetailCommentTableViewCell.h"//评论Cell

#import "OthersHomePageViewController.h"//他人主页Controller

@interface TunerDetailViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@end

@implementation TunerDetailViewController
{
    TunerDetailHeaderView *_headerView;
    
    NSMutableArray *_commentArray;
    
    NSMutableArray *_cellHeightArray;
}

 static NSString *const TunerDetailCommentTableViewCellID = @"TunerDetailCommentTableViewCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNav];
    
    [self createData];
    
    //注册TableView
    [self registerTableView];
    
}
- (void)createData{
    
    
    _commentArray = [NSMutableArray array];
    NSArray *arr = @[
                     @[@"Lister · 傅", @"2015-05-20 16:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲"],
                     @[@"Lister · 傅傅", @"2025-05-20 16:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!"],
                     @[@"Lister · 傅傅傅", @"2025-05-20 26:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!"],
                     @[@"Lister · 傅傅傅傅", @"2015-05-20 16:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!"],
                     @[@"Lister · 傅傅傅傅傅傅", @"2015-05-20 16:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!"],
                     @[@"Lister · 傅傅傅傅傅傅傅", @"2015-05-20 16:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲"],
                     @[@"Lister · 傅傅傅傅傅傅傅傅", @"2025-05-20 26:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲"],
                     @[@"ListerLister · 傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅", @"2015-05-20 16:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!"],
                     @[@"ListerListerLister · 傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅", @"2015-05-20 16:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲"],
                     @[@"ListerListerListerLister · 傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅", @"2015-05-20 16:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲"],
                     @[@"ListerListerListerListerLister · 傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅傅", @"2015-05-20 16:55", @"给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!给你99分,少1分怕你骄傲骄傲骄傲骄傲!"]
                      ];
    [_commentArray addObjectsFromArray:arr];
    
    
    if (!_cellHeightArray) {
        _cellHeightArray = [NSMutableArray array];
    }
    
    for (int i = 0; i < _commentArray.count; i++) {
        [_cellHeightArray addObject:@"0"];
    }
}
- (void)setNav{
    self.title = @"详情";
    
    UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon_home_meitu_1"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationItem.leftBarButtonItem = back;
}
#pragma mark === 注册TableView ===
- (void)registerTableView{
    
    __weak __typeof__ (self) weakSelf = self;
    
    _headerView = [[[NSBundle mainBundle] loadNibNamed:@"TunerDetailHeaderView" owner:nil options:nil] lastObject];
    _headerView.click = ^(int num){
        switch (num) {
            case 0:
                //收藏
                NSLog(@"收藏");
                break;
            case 1:
                //我想改
                NSLog(@"我想改");
                break;
            case 2:
                //评论
                NSLog(@"评论");
                break;
            case 3:
                //分享
                NSLog(@"分享");
                break;
            case 4:
                //商家主页
            {
                OthersHomePageViewController *others = [[OthersHomePageViewController alloc] init];
                others.hidesBottomBarWhenPushed = YES;
                [weakSelf.navigationController pushViewController:others animated:YES];
            }
                break;
            default:
                break;
        }
    };
    self.tableView.tableHeaderView = _headerView;
    
    [self.tableView registerClass:[TunerDetailCommentTableViewCell class] forCellReuseIdentifier:TunerDetailCommentTableViewCellID];
}

#pragma mark === TableViewDelegate ===
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _commentArray.count;
}
//行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [_cellHeightArray[indexPath.row] floatValue];
}
//Reusable
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    TunerDetailCommentTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:TunerDetailCommentTableViewCellID];
    if (!cell) {
        cell = [[TunerDetailCommentTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:TunerDetailCommentTableViewCellID];
        
    }
    [cell setLayout:_commentArray[indexPath.row]];
    
    [_cellHeightArray replaceObjectAtIndex:indexPath.row withObject:cell.height];
    
    cell.headImgClick = ^(){
        NSLog(@"跳到第%ld个用户主页", indexPath.row);
        OthersHomePageViewController *others = [[OthersHomePageViewController alloc] init];
        others.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:others animated:YES];
    };
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView= [[UITableView alloc]initWithFrame:CGRectMake(0,0, Screen_Width, Screen_Height)style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;//去掉滑动竖条
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

@end
